﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class ChangeRepairBox : BasePage
    {
        int boxID;
        int jobID;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (string.IsNullOrEmpty(Request.QueryString[ID]))
            {
                CloseDlg(0);
                return;
            }

            boxID = int.Parse(Request.QueryString[ID]);
            jobID = int.Parse(Request.QueryString[JOB_ID]);

            if (!IsPostBack)
            {
                LoadRepairBoxes();
                ddlRepairBox.SelectedValue = boxID.ToString();
            }
        }

        void LoadRepairBoxes()
        {
            Utilities.LoadDropDownList(ddlRepairBox, View_RepairBoxes.GetAllActiveBoxes().DefaultView, View_RepairBoxes.ColumnNames.Status,
                View_RepairBoxes.ColumnNames.ID);
        }

        public void Save()
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();

            try
            {
                RepairJob job = new RepairJob();
                job.LoadByPrimaryKey(jobID);
                if (job.RowCount > 0)
                {
                    tx.BeginTransaction();
                    int boxID = 0;
                    if (!job.IsColumnNull(View_RepairJobs.ColumnNames.RepairBoxID))
                        boxID = job.RepairBoxID;

                    job.s_RepairBoxID = ddlRepairBox.SelectedValue;
                    job.LastModifiedByUser = SessionUser.ID;
                    job.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                    job.Save();

                    if (boxID > 0)
                        View_RepairJobs.UpdateRepairBoxStatus(boxID);

                    if (!job.IsColumnNull(View_RepairJobs.ColumnNames.RepairBoxID))
                        View_RepairJobs.UpdateRepairBoxStatus(job.RepairBoxID);

                    tx.CommitTransaction();
                }
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                throw ex;
            }
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            try
            {
                Save();
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
                ((Popup)Master).ShowMessage("Saved");
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
                ErrorLog.WriteToFile("ChangeRepairBox.aspx.cs", "btnSave_OnClick", ex.StackTrace);
            }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            try
            {
                Save();
                CloseDlg(1);
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
                ErrorLog.WriteToFile("ChangeRepairBox.aspx.cs", "btnSaveAndClose_OnClick", ex.StackTrace);
            }
        }
    }
}